package sg.edu.nus.iss.vmcs.store;

import java.util.ArrayList;

/*
 * Copyright 2003 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

/**
 *
 *
 * @version 3.0 5/07/2003
 * @author Olivo Miotto, Pang Ping Li
 */
/**
 * @author Zaw (modified)
 * CashStoreItemIterator
 */
public class CashStore extends Store {

	public CashStore(int itemn) {
		super(itemn);
	}

	public final static int INVALID_COIN_WEIGHT = 9999;
	public final static String COIN_RECEIVER_ERR = "COIN_RECEIVER_ERR";
	public final static String COIN_RECEIVER_CLEAR_ERR = "COIN_RECEIVER_CLEAR_ERR";

	public CashStore() {
		super();
	}
	
	public int findCashStoreIndex (Coin c) {
		int size = getStoreSize();
		for (int i = 0; i < size; i++) {
			StoreItem item = (CashStoreItem) getStoreItem(i);
			Coin current = (Coin) item.getContent();
			if (current.getWeight() == c.getWeight())
				return i;
		}
		return -1;
	}
	
	public Coin findCoin(double weight){
		StoreItem[] storeItems = getItems();
		if (storeItems == null)
			return null;
		
		for (int i = 0; i < storeItems.length; i++){
			Coin coin = (Coin) storeItems[i].getContent();
			if (coin.getWeight() == weight)
				return coin;
		}
		return null;
	}


	public StoreItemIterator createCashStoreItemIterator(){
		ArrayList<CashStoreItem> ls = new ArrayList<CashStoreItem>();
		for (int i = 0; i < size; i++) {
			ls.add((CashStoreItem) getStoreItem(i));
		}
		return new CashStoreItemIterator(ls);
	}
	
	public enum SortBy{ Weight, Amount, Quantity, NO_SORT}
	
	public StoreItemIterator createCashStoreItemIterator(SortBy sortBy){
		CashStoreItemIterator itr = (CashStoreItemIterator) createCashStoreItemIterator();
		switch(sortBy){
		case Weight:
			itr.sortByWeight();
			break;
		case Amount:
			itr.sortByAmount();
			break;
		case Quantity:
			itr.sortByQuantity();
			break;
		}
		return itr;
	}

}

